package com.txby.config;

import java.io.File;
import java.util.Arrays;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.txby.utils.WebUtil;

@Configuration
public class MVCConfig extends WebMvcConfigurerAdapter {
    @Resource
    private RouteConfig routeConfig;

	@Bean
	public FilterRegistrationBean indexFilterRegistration() {
		//添加跨域过滤器
		FilterRegistrationBean registration = new FilterRegistrationBean(new CrossFilter());
//		registration.addUrlPatterns("/");
		return registration;
	}
    @Bean
    MyHandlerInterceptor localInterceptor() {
        return new MyHandlerInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }

    
    @Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		 registry.addResourceHandler("/static/**").addResourceLocations("/static/templates/**")
            .addResourceLocations("classpath:/static").addResourceLocations("classpath:/static/templates");
		 registry.addResourceHandler("/static2/**")
            .addResourceLocations("classpath:/static2");
		 registry.addResourceHandler("/show/**").addResourceLocations("file:"+routeConfig.getHardDisk()+ File.separator);
//		registry.addResourceHandler("swagger-ui.html")
//     	.addResourceLocations("classpath:/META-INF/resources/");
//		 registry.addResourceHandler("/webjars/**")
//		.addResourceLocations("classpath:/META-INF/resources/webjars/");
	}
    

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }

}

class MyHandlerInterceptor extends HandlerInterceptorAdapter {


    @Resource
    private RouteConfig routeConfig;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String ip = WebUtil.getIpAddress(request);
        String path = request.getServletPath();
        //不对匹配该值的访问路径拦截（正则）
        final String NO_INTERCEPTOR_PATH = ".*/((show)).*";
        if(path.matches(NO_INTERCEPTOR_PATH)){ //允许展示路径
            return true;
        }else{
            //允许ip可以进行上传访问
          String openIP=routeConfig.getOpenIP()==null?"-1":routeConfig.getOpenIP();    
          if(!openIP.equals("-1")&&openIP.equals("1")){
        	  if(routeConfig.getIPs()!=null&&routeConfig.getIPs().length>0&&Arrays.asList(routeConfig.getIPs()).contains(ip)){
        		  return true;
        	  }else {
        			  return false;
        	  }
          }else{
        	  
        	  return true;
          }
        }
    }



}